import pandas as pd
import pymysql


def read_mysql_to_dataframe(table_name="merge_info"):
    db_config = {
        'host': 'localhost',
        'user': 'root',
        'password': '123456',
        'database': 'food_price'
    }

    # 建立与 MySQL 数据库的连接
    conn = pymysql.connect(**db_config)

    # 查询数据
    query = f"SELECT * FROM {table_name}"
    df = pd.read_sql(query, conn)

    # 关闭数据库连接
    conn.close()

    return df


def read_sql(table_name):
    """
    读取对应的sql文件
    :param table_name:
    :return:
    """
    db_config = {
        'host': 'localhost',
        'user': 'root',
        'password': '123456',
        'database': 'food_price'
    }

    df = read_mysql_to_dataframe(table_name, db_config)

    return df

#
# food_info = read_sql("food_info")
# weather_info = read_sql("weather_info")
#
# # 将date列转换为datetime类型
# weather_info['date'] = pd.to_datetime(weather_info['date'].str.split().str[0])
# # 将日期时间格式化为时分秒形式
# weather_info['pub_time'] = weather_info['date'].dt.strftime('%Y-%m-%d %H:%M:%S')
#
# # 按照pub_time进行拼接
# df_merged = pd.merge(weather_info, food_info, on='pub_time', how='inner')
# df_merged = df_merged.drop(columns=['place', 'date'])
# df_merged['pub_time'] = pd.to_datetime(df_merged['pub_time']).dt.strftime('%Y-%m-%d')
#
# df_merged.to_csv('merge.csv', index=False)
# print("执行完毕")


